package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.File;
import com.ruoyi.system.domain.SysAppVersion;
import com.ruoyi.system.service.ISysAppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = "App")
@RestController
@RequestMapping("/app/version")
public class SysAppVersionController extends BaseController
{
    @Autowired
    private ISysAppVersionService appVersionService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询到对应的上线发行的二维码图片
     */
    @GetMapping("/qr")
    @ApiOperation(value = "二维码图片", response = File.class)
    public void qr(HttpServletResponse response) throws IOException {
        byte[] fileData = appVersionService.selectqr();
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"qr.jpg\"");
        response.addHeader("Content-Length", "" + fileData.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(fileData, response.getOutputStream());
    }


    /**
     * 查询App版本列表
     */
    // @RequiresPermissions("system:appVersion:list")
    @GetMapping("/list")
    @ApiOperation(value = "App版本列表（分页）", response = SysAppVersion.class)
    public TableDataInfo list(SysAppVersion sysAppVersion) {
        startPage();
        List<SysAppVersion> list = appVersionService.selectSysAppVersionList(sysAppVersion);
        return getDataTable(list);
    }


    /**
     * 导出App版本列表
     */
    // @RequiresPermissions("system:appVersion:export")
    @Log(title = "导出App版本列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出App版本列表")
    public void export(HttpServletResponse response, SysAppVersion sysAppVersion) {
        List<SysAppVersion> list = appVersionService.selectSysAppVersionList(sysAppVersion);
        ExcelUtil<SysAppVersion> util = new ExcelUtil<SysAppVersion>(SysAppVersion.class);
        util.exportExcel(response, list, "App版本数据");
    }

    /**
     * 获取App详细信息
     */
    // @RequiresPermissions("system:appVersion:query")
    @GetMapping(value = "/{versionId}")
    @ApiOperation(value = "获取App版本详细信息", response = SysAppVersion.class)
    public AjaxResult getInfo(@ApiParam(value = "versionId", required = true) @PathVariable("versionId") Long versionId) {
        return success(appVersionService.selectSysAppVersionById(versionId));
    }

    /**
     * 获取最新App版本详细信息
     */
    // @RequiresPermissions("system:appVersion:query")
    @GetMapping(value = "/latest")
    @ApiOperation(value = "获取最新App版本详细信息", response = SysAppVersion.class)
    public AjaxResult latest(SysAppVersion appVersion) {
        return success(appVersionService.latest(appVersion));
    }

    /**
     * 新增App
     */
    // @RequiresPermissions("system:appVersion:add")
    @Log(title = "新增App版本", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增App版本")
    public AjaxResult add(@RequestBody SysAppVersion sysAppVersion) throws IOException {
        if(sysAppVersion.getVersionId() == null){
            return toAjax(appVersionService.insertSysAppVersion(sysAppVersion));
        }
        return toAjax(appVersionService.updateSysAppVersion(sysAppVersion));
    }

    /**
     * 修改App
     */
    // @RequiresPermissions("system:appVersion:edit")
    @Log(title = "修改App版本", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改App版本")
    public AjaxResult edit(@RequestBody SysAppVersion sysAppVersion) throws IOException {
        return toAjax(appVersionService.updateSysAppVersion(sysAppVersion));
    }

    /**
     * 删除App
     */
    // @RequiresPermissions("system:appVersion:remove")
    @Log(title = "删除App版本", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{versionIds}")
    @ApiOperation(value = "删除App版本")
    public AjaxResult remove(@ApiParam(value = "versionIds", required = true) @PathVariable Long[] versionIds) {
        return toAjax(appVersionService.deleteSysAppVersionByIds(versionIds));
    }
}
